#include "adc.h"

uint16_t whole_adc_data[2][12];

/**
  * @brief  REIN_ADC综合初始化
  * @param  NULL
  * @retval NULL
**/
void REIN_ADC_Init(void)
{
     /* 1. GPIO配置 */
     rcu_periph_clock_enable(RCU_GPIOA);
     gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1);

     /* 2. DMA配置 */
     dma_parameter_struct dma_init_struct;
     rcu_periph_clock_enable(RCU_DMA0);

     dma_deinit(DMA0, DMA_CH0);
     dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
     dma_init_struct.memory_addr = (uint32_t)whole_adc_data;
     dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
     dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
     dma_init_struct.number = 2;
     dma_init_struct.periph_addr = (uint32_t)&ADC_RDATA(ADC0);
     dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
     dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
     dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
     dma_init(DMA0, DMA_CH0, &dma_init_struct);
     dma_circulation_enable(DMA0, DMA_CH0);

     /* 3. ADC配置 */
     rcu_periph_clock_enable(RCU_ADC0);
     adc_deinit(ADC0);

     // ADC时钟配置（PCLK2分频）
     rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV12);

     adc_mode_config(ADC_MODE_FREE); 
     adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
     adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
     adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);

     // 通道配置
     adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 2);
     adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_239POINT5);
     adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_239POINT5);

     /* 4. 关联ADC和DMA */
     adc_dma_mode_enable(ADC0);

     /* 5. 校准&启动 */
     adc_calibration_enable(ADC0);
     adc_enable(ADC0);
     delay_1ms(1);
     adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
     dma_channel_enable(DMA0, DMA_CH0);

}

